package db
/* package db

import (
	"ULCrawler/conf"
	"database/sql"
	"time"

	_ "github.com/go-sql-driver/mysql"
)

var db *sql.DB

func init() {
	var err error
	db, err = sql.Open("mysql", conf.Conf.MySqlDb)
	if err != nil {
		panic(err)
	}
	if err = db.Ping(); err != nil {
		panic(err.Error())
	}

	db.SetConnMaxLifetime(4 * time.Hour)

}

// 存储每个渠道每天游戏的收入
func SaveDayIncome(as []AppResult) (err error) {
	for _, v := range as {
		_, err = db.Exec(`replace into gamedayincome(dayDate, gameName, channelName, view, click, clickRatio, ecpm, cpc,buyincome,fillingRate, income,account,platform,appId) 
		values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
			v.Date,
			v.AppName,
			v.ChannelName,
			v.View,
			v.Click,
			v.ClickRatio,
			v.Ecpm,
			v.Cpc,
			v.BuyIncome,
			v.FillingRate,
			v.Income,
			v.Account,
			v.Platform,
			v.AppId)
		if err != nil {
			return
		}
	}
	return
}

// 存储每个渠道每天游戏广告位的收入
func SavePositionIncome(ps []PositionResult) (err error) {
	for _, v := range ps {
		_, err = db.Exec(`replace into gamedaypositionincome(dayDate, gameName, channelName, position,positionName, view, click, clickRatio, ecpm, cpc,buyincome,fillingRate, income,account,platform,posId,appId)
			values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
			v.Date,
			v.AppName,
			v.ChannelName,
			v.Position,
			v.PositionName,
			v.View,
			v.Click,
			v.ClickRatio,
			v.Ecpm,
			v.Cpc,
			v.BuyIncome,
			v.FillingRate,
			v.Income,
			v.Account,
			v.Platform,
			v.PosId,
			v.AppId)
		if err != nil {
			return
		}
	}
	return
}

// 储存渠道每天游戏的新增活跃数据
func SaveChannelActive(cs []ChannelActive) (err error) {
	for _, v := range cs {
		_, err = db.Exec(`replace into channelactive(dayDate, appId, gameName, channelName, newUser, activeUser,platform) values (?,?,?,?,?,?,?)`,
			v.Date,
			v.AppId,
			v.AppName,
			v.ChannelName,
			v.NewUser,
			v.ActiveUser,
			v.Platform)
		if err != nil {
			return
		}
	}
	return
}

// 存入数据库通知某个渠道已经采集完成的消息
func SaveMsg(date, channelName string) error {
	var n int
	err := db.QueryRow(`select count(1) from msg where dayDate=? and channelName =?`, date, channelName).Scan(&n)
	if err != nil {
		return err
	}
	if n == 0 {
		_, err = db.Exec(`insert into msg(dayDate, channelName) values(?,?)`, date, channelName)
	}
	return err
}
 */